layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'member_list',
		form_filter = 'member_search_form';
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: member_list_url,
		method: 'post',
		height: 'full-180',
		where: {
			_o: 'last_login_time',
			_b: 'desc'
		},
		cols: [member_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		}
	});

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	// select 查询
	form.on('select(site_id)', function(data) {
		var value = data.value;
		var where = {};
		where['site_id'] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	})
	// 顶部删除按钮
	$('.del-staff').on('click', function() {
		var checkStatus = table.checkStatus(table_filter);
		var tr = checkStatus.tr;
		if(checkStatus.data.length == 0) {
			layer.msg('请选择要删除的数据', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		var staff_id_arr = new Array();
		layer.confirm('确定删除选中数据吗？', function(index) {
			checkStatus.data.map(function(obj, i) {
				staff_id_arr.push(obj.staff_id);
			});
			send_info(staff_delete_url, {
				staff_id: staff_id_arr
			}, function(data) {
				if(data.code == 0) {
					tr.map(function(obj, i) {
						$(obj.selector).remove();
					})
					layer.msg('删除成功', {
						icon: 1
					});
					// 重载回收站
					if($('table[lay-filter="recycle_staff_list"]').length > 0) {
						table.reload('recycle_staff_list');
					}
					layer.close();
				} else {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				}
			})
		})
	})
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var staff_id = data.staff_id;
		switch(e) {
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(staff_delete_url, {
						staff_id: staff_id
					}, function(data) {
						if(data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if($('table[lay-filter="recycle_staff_list"]').length > 0) {
								table.reload('recycle_staff_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'frozen':
				// 删除
				layer.confirm('冻结改账号后，该账号将不能登录，确定冻结吗？', function(index) {
					send_info(member_save_url, {
						member_id: staff_id,
						state:2
					}, function(data) {
						if(data.code == 0) {
							layer.msg('账号冻结成功', {
								icon: 1
							});
							tr.find('.hide-update').trigger('click');
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'refrozen':
				// 删除
				layer.confirm('确定解冻该账号吗？', function(index) {
					send_info(member_save_url, {
						member_id: staff_id,
						state:1
					}, function(data) {
						if(data.code == 0) {
							layer.msg('账号解冻成功', {
								icon: 1
							});
							tr.find('.hide-update').trigger('click');
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(staff_one_url, {
					staff_id: data.staff_id
				}, function(data) {
					delete data.data.staff_id;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	})
})